function statPLIBasic(inDir,outDir)
%inDir='/Users/matthew/workspace/mfiles/projects/data/VNSpliXband/8sec/'
%outDir='/Users/matthew/workspace/mfiles/projects/data/VNSpliXband/8sec/'
filter='pliXsubjs*';
files=dir(fullfile(inDir,filter));
nBands=size(files,1);
alphalevel=0.05;

for i=1:nBands
  
    load(fullfile(inDir,files(i).name));
    stat=statOverSubjs(cond_one,cond_two,getAreas(),alphalevel);
    stat.arealabels=getAreaLabels();
    [res,outfile]=strtok(files(i).name,'_');
    outfile=strcat('stat',outfile);
    save(fullfile(outDir,outfile),'stat');
    
end


function [arealabels]=getAreaLabels()
arealabels={'lf';'lc';'lt';'lp';'lo';'rf';'rc';'rt';'rp';'ro';'zz'};
%arealabels={'lf';'lc';'lp';'rf';'rc';'rp';'zz'};
%arealabels={'Fp1';'Fp2';'F7';'F3';'Fz';'F4';'F8';'T3';'C3';'Cz';'C4';'T4' ;'T5';'P3';'Pz';'P4';'T6';'O1';'O2'};
function [areas]=getAreas()
lf=[1 3 4];
lc=9;
lt=[8 13];
lp=14;
lo=18;

rf=[2 6 7];
rc=11;
rt=[12 17];
rp=16;
ro=19;

zz=[5 10 15];

areas={lf;lc;lt;lp;lo;rf;rc;rt;rp;ro;zz};

%labels={'Fp1','Fp2','F7','F3','Fz','F4','F8','T3','C3','Cz','C4','T4' ,'T5','P3','Pz','P4','T6','O1','O2'};

% lf=[1 3 4];
% lc=[8 9];
% lp=[13 14 18];
% 
% rf=[2 6 7];
% rc=[11 12];
% rp=[16 17 19]
% 
% zz=[5 10 15];

%areas={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};


function [stat]=statOverSubjs(cond_one,cond_two,areas,alphalevel)


interAreaLen=(length(areas)*(length(areas)-1))/2;

nSubjs=size(cond_one,1);

subjXinterArea_one=zeros(nSubjs,interAreaLen)
subjXinterArea_two=zeros(nSubjs,interAreaLen)

nch=size(cond_one,2);
indx=ones(nch,nch);
indx=indx-tril(indx);
indx=logical(indx);

subjXavgpli_one=zeros(nSubjs,1);
subjXavgpli_two=zeros(nSubjs,1);


for i=1:nSubjs
    m_one=squeeze(cond_one(i,:,:));
    m_two=squeeze(cond_two(i,:,:));
    
    subjXinterArea_one(i,:)=longPLI(m_one,areas);
    subjXinterArea_two(i,:)=longPLI(m_two,areas);
    
    subjXavgpli_one(i)=mean(m_one(indx));
    subjXavgpli_two(i)=mean(m_two(indx));
end

stat.inter={subjXinterArea_one,subjXinterArea_two};
stat.avg={ subjXavgpli_one, subjXavgpli_two};

[stat.h_inter,stat.p_inter]=ttest(subjXinterArea_one, subjXinterArea_two,alphalevel);
[stat.h_avgpli,stat.p_avgpli]=ttest(subjXavgpli_one,subjXavgpli_two,alphalevel);

stat.areas=areas;

function [interArea]=longPLI(pli,areas)

v=zeros(length(areas),length(areas));
for i=1:length(areas)-1
    for j=i+1:length(areas)
        m=pli(areas{i},areas{j});
        s=size(m);
        v(i,j)=mean(reshape(m,s(1)*s(2),1));
    end
end

indx=ones(size(v));
indx=indx-tril(indx);
indx=logical(indx);
%indx=logical(indx);%%warning if phase is 0
interArea=v(indx)';






